from dataclasses import dataclass
import os
import zipfile
import glob
import pandas as pd

#创建缓存文件
PYINCORE_PACKAGE_HOME = os.path.dirname(__file__)
PYINCORE_ROOT_FOLDER = os.path.dirname(os.path.dirname(__file__))
USER_HOME = os.path.expanduser('~')
USER_CACHE_DIR = ".incoretest2"
PYINCORE_USER_CACHE = os.path.join(USER_HOME, USER_CACHE_DIR)
DATA_CACHE_FOLDER_NAME = "cache_data"
PYINCORE_USER_DATA_CACHE = os.path.join(PYINCORE_USER_CACHE, DATA_CACHE_FOLDER_NAME)


def get_dataset_blob(dataset_id: str):
       
    local_filename = None

    # 构造本地目录和文件名
    cache_data = PYINCORE_USER_DATA_CACHE
    if not os.path.exists(cache_data):
        os.makedirs(cache_data)

    # 添加带id名称的子文件夹名称
    cache_data_dir = os.path.join(cache_data, dataset_id)

    # 创建上述名称的文件夹
    if not os.path.exists(cache_data_dir):
        os.makedirs(cache_data_dir)
        local_filename = r'C:\Users\29968\.incoretest2\cache_data\11234\56789.zip'

    # 检查是否存在zip和id文件
    else:
        for fname in os.listdir(cache_data_dir):
            if fname.endswith('.zip'):
                local_filename = os.path.join(cache_data_dir, fname)
                print('Dataset already exists locally. Reading from local cached zip.')
        if not local_filename:
            local_filename = r'C:\Users\29968\.incoretest2\cache_data\11234\56789.zip'
    
    folder = unzip_dataset(local_filename)
    if folder is not None:
        return folder
    else:
        return local_filename


#解压zip文件
def unzip_dataset(local_filename: str):
    
    foldername, file_extension = os.path.splitext(local_filename)
    # 判断有没有zip
    if not file_extension.lower() == '.zip':
        print('It is not a zip file; no unzip')
        return None
    # 判断有没有文件夹
    if os.path.isdir(foldername):
        print('Unzipped folder found in the local cache. Reading from it...')
        return foldername
    os.makedirs(foldername)

    zip_ref = zipfile.ZipFile(local_filename, 'r')
    zip_ref.extractall(foldername)
    zip_ref.close()
    return foldername

def cache_files(dataset_id: str):
    local_file_path = get_dataset_blob(dataset_id)
    return local_file_path


def get_file_path(type='csv'):
        filename = cache_files('11234')
        if os.path.isdir(filename):
            files = glob.glob(filename + "/*." + type)
            if len(files) > 0:
                filename = files[0]

        return filename

def get_dataframe_from_csv():
    
    filename = get_file_path('csv')
    df = pd.DataFrame()
    if os.path.isfile(filename):
        df = pd.read_csv(filename, header="infer")
    return df

def get_output_dataset(self, id):
    return self.output_datasets[id]['value']

abss = get_dataframe_from_csv()
print(abss)



